package cn.tutu.blockchain.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.tutu.blockchain.common.MsgEnum;
import cn.tutu.blockchain.common.MsgObj;
import cn.tutu.shop.entity.Region;
import cn.tutu.shop.service.RegionService;

@RestController
@RequestMapping("/region")
public class RegionController {
	private static Logger LOGGER = LoggerFactory.getLogger(RegionController.class);
	@Autowired
	private RegionService regionService;

	@RequestMapping("/getByParentId")
	public MsgObj getByParentId(String parentId) {
		LOGGER.info("========根据父类ID获取区域信息,父类ID：{}======", parentId);

		if (StringUtils.isEmpty(parentId)) {
			return new MsgObj(MsgEnum.PARAM_ERROR.getCode(), MsgEnum.PARAM_ERROR.getMessage(), null);
		}
		try {
			List<Region> listMcArea = regionService.getByParentId(parentId);
			return MsgObj.buildSuccessMsg(MsgEnum.SUCCESS.getCode(), "成功", listMcArea);
		} catch (Exception e) {
			LOGGER.error("========根据父类ID获取区域信息,父类ID：{}======", parentId, e);
			e.printStackTrace();
			return MsgObj.buildExceptionMsg(e);
		}
	}

	@RequestMapping("/getById")
	public MsgObj getById(String regionId) {
		LOGGER.info("========根据ID获取区域信息,ID：{}======", regionId);

		if (StringUtils.isEmpty(regionId)) {
			return new MsgObj(MsgEnum.PARAM_ERROR.getCode(), MsgEnum.PARAM_ERROR.getMessage(), null);
		}
		try {
			Region region = regionService.getById(regionId);
			return MsgObj.buildSuccessMsg(MsgEnum.SUCCESS.getCode(), "成功", region);
		} catch (Exception e) {
			LOGGER.error("========根据ID获取区域信息,父类ID：{}======", regionId, e);
			e.printStackTrace();
			return MsgObj.buildExceptionMsg(e);
		}
	}

	@RequestMapping("/getAll")
	public MsgObj getAll() {
		List<Region> list = regionService.getAll();
		return MsgObj.buildSuccessMsg(list);

	}

}
